package org.jeecg.modules.rental.controller;

import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.rental.entity.InvestorProfits;
import org.jeecg.modules.rental.resp.InvestorProfitsResponse;
import org.jeecg.modules.rental.service.IInvestorProfitsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 投资方分润表
 * @Author: jeecg-boot
 * @Date:   2025-02-11
 * @Version: V1.0
 */
@Api(tags="投资方分润表")
@RestController
@RequestMapping("/rental/investorProfits")
@Slf4j
public class InvestorProfitsController extends JeecgController<InvestorProfits, IInvestorProfitsService> {
	@Autowired
	private IInvestorProfitsService investorProfitsService;
	
	/**
	 * 分页列表查询
	 *
	 * @param investorProfits
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "投资方分润表-分页列表查询")
	@ApiOperation(value="投资方分润表-分页列表查询", notes="投资方分润表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<InvestorProfits>> queryPageList(InvestorProfits investorProfits,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<InvestorProfits> queryWrapper = QueryGenerator.initQueryWrapper(investorProfits, req.getParameterMap());
		Page<InvestorProfits> page = new Page<InvestorProfits>(pageNo, pageSize);
		IPage<InvestorProfits> pageList = investorProfitsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param investorProfits
	 * @return
	 */
	@AutoLog(value = "投资方分润表-添加")
	@ApiOperation(value="投资方分润表-添加", notes="投资方分润表-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:investor_profits:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody InvestorProfits investorProfits) {
		investorProfitsService.save(investorProfits);
		return Result.OK("添加成功！");
	}


	 @ApiOperation("查询投资方分润数据")
	 @GetMapping("/queryInvestorProfits")
	 public Result<IPage<InvestorProfitsResponse>> queryInvestorProfits(
			 @RequestParam(value = "code", required = false) String code,
			 @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
			 @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
			 @RequestParam(defaultValue = "1") Integer pageNo, // 分页页码，默认为第 1 页
			 @RequestParam(defaultValue = "10") Integer pageSize // 每页大小，默认为 10
	 ) {
		 try {
			 // 构建查询条件
			 QueryWrapper<InvestorProfits> queryWrapper = new QueryWrapper<>();
			 if (code != null) {
				 queryWrapper.eq("code", code); // 根据投资编号筛选
			 }
			 if (startDate != null) {
				 queryWrapper.ge("starttime", startDate); // 开始时间筛选
			 }
			 if (endDate != null) {
				 queryWrapper.le("endtime", endDate); // 结束时间筛选
			 }

			 // 分页对象
			 Page<InvestorProfits> page = new Page<>(pageNo, pageSize);

			 // 分页查询数据库中的记录
			 IPage<InvestorProfits> profitsPage = investorProfitsService.page(page, queryWrapper);

			 // 将查询结果转换为自定义返回对象
			 IPage<InvestorProfitsResponse> responsePage = profitsPage.convert(profit -> {
				 InvestorProfitsResponse response = new InvestorProfitsResponse();
				 response.setCode(profit.getCode()); // 投资编号
				 response.setDate(profit.getProfitTime() != null ? profit.getProfitTime().toString() : null); // 收益时间
				 response.setAreaRevenue(profit.getAreaRevenue()); // 区域收益
				 response.setNum(profit.getNum()); // 投资数量
				 response.setProfit(profit.getIncome() != null ? new java.math.BigDecimal(profit.getIncome()) : null); // 我的分润
				 return response;
			 });

			 // 返回分页结果
			 return Result.OK(responsePage);
		 } catch (Exception e) {
			 log.error("查询投资方分润数据失败", e);
			 return Result.error("查询失败：" + e.getMessage());
		 }
	 }
	/**
	 *  编辑
	 *
	 * @param investorProfits
	 * @return
	 */
	@AutoLog(value = "投资方分润表-编辑")
	@ApiOperation(value="投资方分润表-编辑", notes="投资方分润表-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:investor_profits:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody InvestorProfits investorProfits) {
		investorProfitsService.updateById(investorProfits);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "投资方分润表-通过id删除")
	@ApiOperation(value="投资方分润表-通过id删除", notes="投资方分润表-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:investor_profits:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		investorProfitsService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "投资方分润表-批量删除")
	@ApiOperation(value="投资方分润表-批量删除", notes="投资方分润表-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:investor_profits:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.investorProfitsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "投资方分润表-通过id查询")
	@ApiOperation(value="投资方分润表-通过id查询", notes="投资方分润表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<InvestorProfits> queryById(@RequestParam(name="id",required=true) String id) {
		InvestorProfits investorProfits = investorProfitsService.getById(id);
		if(investorProfits==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(investorProfits);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param investorProfits
    */
    //@RequiresPermissions("org.jeecg.modules.demo:investor_profits:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, InvestorProfits investorProfits) {
        return super.exportXls(request, investorProfits, InvestorProfits.class, "投资方分润表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("investor_profits:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, InvestorProfits.class);
    }

}
